﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Data.OleDb;
using ADOX;
using System.Drawing;

namespace Grizzy_X
{
    static class Persistance
    {
        static public string mdbPath;
        static public string dbConnectionString;
        public static OleDbConnection myConnection ;
        public static OleDbCommand cmd;
        public static OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();

        static public bool Openfiledialog() 
        {
            try
            {
                OpenFileDialog dlgopen = new OpenFileDialog();
                dlgopen.Title = "Open File ";
                dlgopen.Filter = "JPG(*.JPG;*.JPEG);gif(*.GIF);位图(*.BMP)|*.jpg;*.jpeg;*.gif;*.bmp;*.png"+"|All Files|*.*";
                if (dlgopen.ShowDialog() == DialogResult.OK)
                {
                    Controller.filename = dlgopen.FileName;
                    Controller.check = true;
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false ;
            }
        }
        static public bool OpenMap()
        {
            try
            {
                OpenFileDialog openMap = new OpenFileDialog();
                openMap.Title = "导入地图";
                openMap.Filter = "gzx(地图文件)|*.gzx" + "|All Files(所有文件)|*.*";
                if (openMap.ShowDialog() == DialogResult.OK)
                {
                    Controller.check = true;
                    FileStream openmap = new FileStream(openMap.FileName, FileMode.Open);
                    StreamReader readmap = new StreamReader(openmap);
                    string readLine = readmap.ReadLine();
                    readLine.Trim();
                    char separtor = ' ';
                    string[] Fields;
                    Fields = readLine.Split(separtor);
                    string MapName = Fields[0];
                    readLine = readmap.ReadLine();   //比例尺
                    readLine = readmap.ReadLine();   //地图的长、宽
                    Fields = readLine.Split(separtor);
                    double height = Convert.ToDouble(Fields[3]);
                    double width = Convert.ToDouble(Fields[1]);
                    readLine = readmap.ReadLine();
                    Fields = readLine.Split(separtor);
                    string Unit = Fields[1];
                    string Des=readmap.ReadLine();
                    string Path = readmap.ReadLine();
                    CMap NewMap = new CMap();
                    NewMap.New(MapName,height,width,Path,Des,Unit);
                    Controller.MyMap = NewMap;   //赋值给Controller.MyMap
                    readLine = readmap.ReadLine();
                    int LengendNum = Convert.ToInt32(readLine);
                    for (int i = 0; i < LengendNum;i++ )
                    {
                        readLine = readmap.ReadLine();
                        Fields = readLine.Split(separtor);
                        if (Fields[0] == "CPointLegend")
                        {
                            CPointLegend temp = new CPointLegend();
                            temp.SetID(IDGenerator.MapLegendID());
                            temp.Legend_Type = CLegend.LegendType.CPointLegend;
                            temp.Title = Fields[2];
                            temp.Subtitle=Fields[3];
                            temp.Visible = Convert.ToBoolean(Fields[4]);
                            temp.ByScale = Convert.ToBoolean(Fields[5]);
                            if (Fields[6] == "circle")
                                temp.Point_Style = CPointLegend.PointStyle.circle;
                            else if (Fields[6] == "filled_circle")
                                temp.Point_Style = CPointLegend.PointStyle.filled_circle;
                            else if (Fields[6] == "rectangle")
                                temp.Point_Style = CPointLegend.PointStyle.rectangle;
                            else if (Fields[6] == "filled_rectangle")
                                temp.Point_Style = CPointLegend.PointStyle.filled_rectangle;
                            else if (Fields[6] == "triangle")
                                temp.Point_Style = CPointLegend.PointStyle.triangle;
                            else if (Fields[6] == "filled_triangle")
                                temp.Point_Style = CPointLegend.PointStyle.filled_triangle;
                            temp.color = Color.FromArgb(Convert.ToInt32(Fields[7]));
                            temp.size = Convert.ToSingle(Fields[8]);

                            Bitmap preview = new Bitmap((int)(temp.size) + 2, (int)(temp.size) + 2);//画笔默认为两个像素的宽度
                            Graphics gra = Graphics.FromImage(preview);
                            Pen pen = new Pen(temp.color, 2);
                            SolidBrush brush = new SolidBrush(temp.color);
                            Point[] points = new Point[4];
                            //三角形坐标
                            points[0].X = (int)(0.5 * temp.size); points[0].Y = (int)(temp.size - Math.Sqrt(3) * 0.5 * temp.size);
                            points[1].X = (int)(temp.size); points[1].Y = (int)(temp.size);
                            points[2].X = 0; points[2].Y = (int)(temp.size);
                            points[3] = points[0];
                            float tempx;
                            tempx = (float)(temp.size);
                            if (temp.Point_Style == CPointLegend.PointStyle.circle)
                                gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                            else if (temp.Point_Style == CPointLegend.PointStyle.filled_circle)
                                gra.FillEllipse(brush, 0, 0, tempx, tempx);
                            else if (temp.Point_Style == CPointLegend.PointStyle.rectangle)
                                gra.DrawRectangle(pen, 0, 0, temp.size, temp.size);
                            else if (temp.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                                gra.FillRectangle(brush, 0, 0, tempx, tempx);
                            else if (temp.Point_Style == CPointLegend.PointStyle.triangle)
                                gra.DrawLines(pen, points);
                            else if (temp.Point_Style == CPointLegend.PointStyle.filled_triangle)
                                gra.FillPolygon(brush, points);
                            else
                                gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                            temp.cpointlegend = preview;
                            Controller.MapLegends.Add(temp);
                        }
                        else if (Fields[0] == "CLineLegend")
                        {
                            CLineLegend temp = new CLineLegend();
                            temp.SetID(IDGenerator.MapLegendID());
                            temp.Legend_Type = CLegend.LegendType.CLineLegend;
                            temp.Title = Fields[2];
                            temp.Subtitle = Fields[3];
                            temp.Visible = Convert.ToBoolean(Fields[4]);
                            temp.ByScale = Convert.ToBoolean(Fields[5]);
                            if (Fields[6] == "dashline")
                                temp.Line_Style = CLineLegend.LineStyle.dashline;
                            else if (Fields[6] == "solidline")
                                temp.Line_Style = CLineLegend.LineStyle.solidline;
                            temp.color = Color.FromArgb(Convert.ToInt32(Fields[7]));
                            temp.line_weight = Convert.ToSingle(Fields[8]);
                            Controller.MapLegends.Add(temp);
                        }
                        else if (Fields[0] == "CPolygonLegend")
                        {
                            CPolygonLegend temp = new CPolygonLegend();
                            temp.SetID(IDGenerator.MapLegendID());
                            temp.Legend_Type = CLegend.LegendType.CPolygonLegend;
                            temp.Title = Fields[2];
                            temp.Subtitle = Fields[3];
                            temp.Visible = Convert.ToBoolean(Fields[4]);
                            temp.ByScale = Convert.ToBoolean(Fields[5]);
                            if (Fields[6] == "dashline")
                                temp.Border_Style = CPolygonLegend.BorderStyle.dashline;
                            else if (Fields[6] == "solidline")
                                temp.Border_Style = CPolygonLegend.BorderStyle.solidline;

                            if (Fields[7] == "solid")
                                temp.Fill_Style = CPolygonLegend.FillStyle.solid;
                            else if (Fields[7] == "transparent")
                                temp.Fill_Style = CPolygonLegend.FillStyle.transparent;
                            temp.border_weight = Convert.ToSingle(Fields[8]);
                            temp.border_color = Color.FromArgb(Convert.ToInt32(Fields[9])) ;
                            temp.fill_color = Color.FromArgb(Convert.ToInt32(Fields[10])) ;
                            Controller.MapLegends.Add(temp);
                        }
                        else if (Fields[0] == "CTextLegend")
                        {
                            CTextLegend temp = new CTextLegend();
                            temp.SetID(IDGenerator.MapLegendID());
                            temp.Legend_Type = CLegend.LegendType.CTextLegend;
                            temp.Title = Fields[2];
                            temp.Subtitle = Fields[3];
                            temp.Visible = Convert.ToBoolean(Fields[4]);
                            temp.ByScale = Convert.ToBoolean(Fields[5]);
                            temp.string_Color = Color.FromArgb(Convert.ToInt32(Fields[6]));
                            temp.font = new Font(Fields[7],Convert.ToSingle(Fields[8]));
                            Controller.MapLegends.Add(temp);
                        }
                    }
                    readLine = readmap.ReadLine();
                    Fields = readLine.Split(separtor);
                    int LayerNum = Convert.ToInt32(Fields[1]);
                    for (int i = 0; i < LayerNum; i++)
                    {
                        readLine = readmap.ReadLine();
                        Fields = readLine.Split(separtor);
                        ReadLayerInMap(Path + "\\" + MapName + "\\" + Fields[1] + ".lyr");
                    }
                }
                Controller.DBExists = true;
                dbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Controller.MyMap.MapPath +"\\" +Controller.MyMap.Name +"\\test.mdb";
                myConnection = new OleDbConnection(dbConnectionString);
                cmd = myConnection.CreateCommand();
                return true;
            }
            catch
            {
                return false;
            }
        }

        static public bool OpenLayer()
        {
            try
            {
                OpenFileDialog openLayer = new OpenFileDialog();
                openLayer.Title = "导入图层";
                openLayer.Filter = "lyr(图层文件)|*.lyr" + "|All Files(所有文件)|*.*";
                if(openLayer.ShowDialog()==DialogResult.OK)
                {
                    ReadLayer(openLayer.FileName);
                    Controller.check = true;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        static public bool ReadLayer(string filename)
        {
            try
            {
                FileStream openLyr = new FileStream(filename, FileMode.Open);
                StreamReader readlyr = new StreamReader(openLyr);
                string strLine;
                string Name;
                string Des;
                string Type;
                int TotalNum;
                strLine = readlyr.ReadLine();
                strLine.Trim();
                char separtor = ' ';
                string[] tempFileds;
                tempFileds = strLine.Split(separtor);
                Name = tempFileds[0];                 //读出图层的名称
                Des = readlyr.ReadLine();            //读出地图类型
                Type = readlyr.ReadLine();             //读出地图描述
                TotalNum = Convert.ToInt32(readlyr.ReadLine());//读出实体个数
                if (Type == "点图层")
                {
                    CPointLayer newpointLayer = new CPointLayer();
                    newpointLayer.NewPointLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newpointLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CPoint newPoint = new CPoint();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        CPointLegend newLegend = new CPointLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        newPoint.newCpoint(IDGenerator.PointID(), Controller.CurrentLegendIndex);

                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newPoint.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newPoint.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newPoint.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newPoint.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            strLine = readlyr.ReadLine();
                            tempFileds = strLine.Split(separtor);
                            newPoint.points.Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                        }
                        newpointLayer.Point_Set.Add(newPoint);
                    }
                    Controller.MyMap.Add(newpointLayer);
                }
                else if (Type == "线图层")
                {
                    CLineLayer newlineLayer = new CLineLayer();
                    newlineLayer.NewLineLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newlineLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CLine newLine = new CLine();

                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        CLineLegend newLegend = new CLineLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        newLine.newCline(IDGenerator.LineID(), Controller.CurrentLegendIndex);


                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newLine.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newLine.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newLine.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newLine.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            newLine.Lines.Add(new List<PointF>());
                            strLine = readlyr.ReadLine();
                            while (strLine != "0")
                            {
                                tempFileds = strLine.Split(separtor);
                                newLine.Lines[j].Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                                strLine = readlyr.ReadLine();
                            }
                        }
                        newlineLayer.Line_Set.Add(newLine);
                    }
                    Controller.MyMap.Add(newlineLayer);
                }
                else if (Type == "多边形图层")
                {
                    CPolygonLayer newpolyLayer = new CPolygonLayer();
                    newpolyLayer.NewPolygonLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newpolyLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CPolygon newPolygon = new CPolygon();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        CPolygonLegend newLegend = new CPolygonLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        newPolygon.newCpolygon(IDGenerator.PolygonID(), Controller.CurrentLegendIndex);

                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newPolygon.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newPolygon.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newPolygon.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newPolygon.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            newPolygon.Polygons.Add(new List<PointF>());
                            strLine = readlyr.ReadLine();
                            while (strLine != "0")
                            {
                                tempFileds = strLine.Split(separtor);
                                newPolygon.Polygons[j].Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                                strLine = readlyr.ReadLine();
                            }
                        }
                        newpolyLayer.Polygon_Set.Add(newPolygon);
                    }
                    Controller.MyMap.Add(newpolyLayer);
                }
                else if (Type == "文本图层")
                {
                    CTextLayer newtextLayer = new CTextLayer();
                    newtextLayer.NewTextLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newtextLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CText newText = new CText();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        CTextLegend newLegend = new CTextLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;
                        newText.newCtext(IDGenerator.TextID(), Controller.CurrentLegendIndex);

                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newText.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newText.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newText.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newText.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            strLine = readlyr.ReadLine();
                            tempFileds = strLine.Split(separtor);
                            PointF temp = new PointF(Convert.ToSingle(tempFileds[1]), Convert.ToSingle(tempFileds[2]));
                            newText.addpoint_text(temp, tempFileds[0]);
                        }
                        newtextLayer.Text_Set.Add(newText);
                    }
                    Controller.MyMap.Add(newtextLayer);
                }
                openLyr.Dispose();
                openLyr.Close();
                readlyr.Close();
                readlyr.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

        static public bool ReadLayerInMap(string filename)
        {
            try
            {
                FileStream openLyr = new FileStream(filename, FileMode.Open);
                StreamReader readlyr = new StreamReader(openLyr);
                string strLine;
                string Name;
                string Des;
                string Type;
                int TotalNum;
                strLine = readlyr.ReadLine();
                strLine.Trim();
                char separtor = ' ';
                string[] tempFileds;
                tempFileds = strLine.Split(separtor);
                Name = tempFileds[0];                 //读出图层的名称
                Des = readlyr.ReadLine();            //读出地图类型
                Type = readlyr.ReadLine();             //读出地图描述
                TotalNum = Convert.ToInt32(readlyr.ReadLine());//读出实体个数
                if (Type == "点图层")
                {
                    CPointLayer newpointLayer = new CPointLayer();
                    newpointLayer.NewPointLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newpointLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CPoint newPoint = new CPoint();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        int LegendID = Convert.ToInt32(tempFileds[2]);  //获取图例ID
                      /*CPointLegend newLegend = new CPointLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;*/

                        newPoint.newCpoint(IDGenerator.PointID(), LegendID);
                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newPoint.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newPoint.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newPoint.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newPoint.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            strLine = readlyr.ReadLine();
                            tempFileds = strLine.Split(separtor);
                            newPoint.points.Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                        }
                        newpointLayer.Point_Set.Add(newPoint);
                    }
                    Controller.MyMap.Add(newpointLayer);
                }
                else if (Type == "线图层")
                {
                    CLineLayer newlineLayer = new CLineLayer();
                    newlineLayer.NewLineLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newlineLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CLine newLine = new CLine();

                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        int LegendID = Convert.ToInt32(tempFileds[2]);  //获取图例ID
                        /*CLineLegend newLegend = new CLineLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;*/
                        newLine.newCline(IDGenerator.LineID(), LegendID);


                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newLine.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newLine.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newLine.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newLine.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            newLine.Lines.Add(new List<PointF>());
                            strLine = readlyr.ReadLine();
                            while (strLine != "0")
                            {
                                tempFileds = strLine.Split(separtor);
                                newLine.Lines[j].Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                                strLine = readlyr.ReadLine();
                            }
                        }
                        newlineLayer.Line_Set.Add(newLine);
                    }
                    Controller.MyMap.Add(newlineLayer);
                }
                else if (Type == "多边形图层")
                {
                    CPolygonLayer newpolyLayer = new CPolygonLayer();
                    newpolyLayer.NewPolygonLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newpolyLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CPolygon newPolygon = new CPolygon();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        int LegendID = Convert.ToInt32(tempFileds[2]);  //获取图例ID

                       /* CPolygonLegend newLegend = new CPolygonLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;*/
                        newPolygon.newCpolygon(IDGenerator.PolygonID(), LegendID);

                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newPolygon.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newPolygon.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newPolygon.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newPolygon.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            newPolygon.Polygons.Add(new List<PointF>());
                            strLine = readlyr.ReadLine();
                            while (strLine != "0")
                            {
                                tempFileds = strLine.Split(separtor);
                                newPolygon.Polygons[j].Add(new PointF(Convert.ToSingle(tempFileds[0]), Convert.ToSingle(tempFileds[1])));
                                strLine = readlyr.ReadLine();
                            }
                        }
                        newpolyLayer.Polygon_Set.Add(newPolygon);
                    }
                    Controller.MyMap.Add(newpolyLayer);
                }
                else if (Type == "文本图层")
                {
                    CTextLayer newtextLayer = new CTextLayer();
                    newtextLayer.NewTextLayer(IDGenerator.LayerID(), Name, Controller.MapPath, Des);
                    newtextLayer.AttriFields = new List<string[]>();
                    for (int i = 0; i < TotalNum; i++)
                    {
                        CText newText = new CText();
                        int ObjectNum;
                        strLine = readlyr.ReadLine();
                        tempFileds = strLine.Split(separtor);
                        ObjectNum = Convert.ToInt32(tempFileds[1]);  //获取实体个数
                        int LegendID = Convert.ToInt32(tempFileds[2]);  //获取图例ID

                     /* CTextLegend newLegend = new CTextLegend();
                        newLegend.SetID(IDGenerator.MapLegendID());
                        newLegend.initialize();
                        Controller.MapLegends.Add(newLegend);
                        Controller.CurrentLegendIndex = Controller.MapLegends.Count - 1;*/
                        newText.newCtext(IDGenerator.TextID(), LegendID);

                        strLine = readlyr.ReadLine();      //读外包矩形
                        tempFileds = strLine.Split(separtor);
                        newText.MinBR.Lx = Convert.ToSingle(tempFileds[0]);
                        newText.MinBR.Ly = Convert.ToSingle(tempFileds[1]);
                        newText.MinBR.Rx = Convert.ToSingle(tempFileds[2]);
                        newText.MinBR.Ry = Convert.ToSingle(tempFileds[3]);
                        for (int j = 0; j < ObjectNum; j++)
                        {
                            strLine = readlyr.ReadLine();
                            tempFileds = strLine.Split(separtor);
                            PointF temp = new PointF(Convert.ToSingle(tempFileds[1]), Convert.ToSingle(tempFileds[2]));
                            newText.addpoint_text(temp, tempFileds[0]);
                        }
                        newtextLayer.Text_Set.Add(newText);
                    }
                    Controller.MyMap.Add(newtextLayer);
                }
                openLyr.Dispose();
                openLyr.Close();
                readlyr.Close();
                readlyr.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }


        static public bool CreateDirectory()
        {
            string Path = Controller.MapPath + "\\" + Controller.mapname;
            if (Directory.Exists(Path))
            {
                DialogResult result=MessageBox.Show("该地图文件已存在,是否替换？","是否替换",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    Directory.Delete(Path);
                    Directory.CreateDirectory(Path);
                }
                else
                {
                    MessageBox.Show("请重新选择路径！");
                    FolderBrowserDialog MapBrowse = new FolderBrowserDialog();
                    if (MapBrowse.ShowDialog() == DialogResult.OK)
                    {
                        Controller.MapPath=MapBrowse.SelectedPath;
                        Path = Controller.MapPath + "\\" + Controller.mapname;
                        Directory.CreateDirectory(Path);
                        //MessageBox.Show("文件夹创建成功");
                    }
                }
            }
            else
            {
                Directory.CreateDirectory(Path);
                //MessageBox.Show("文件夹创建成功");
            }
            Controller.MapPath = Path;   //此时MapPath已经确定，故修改一下
            return true;
        }

        static public bool ImportBaseMap()
        {
            return true;
        }

        static public bool ExportMap()
        {
            double displayscle=Transformer.Displayscale;
            double offx = Transformer.OffsetX;
            double offy = Transformer.OffsetY;
            Transformer.Displayscale = 1;
            Transformer.OffsetX = 0;
            Transformer.OffsetY = Controller.MyMap.Height;
            SaveFileDialog savebmp = new SaveFileDialog();
            savebmp.Title = "保存文件名";
            savebmp.CheckFileExists = false;
            savebmp.CheckPathExists = false;
            savebmp.Filter = "bmp 文件(*.bmp)|*.bmp|JPEG 文件(*.jpg)|*.jpg";
            if (savebmp.ShowDialog() == DialogResult.OK)
            {
                //图的大小有问题啊！
                Bitmap newBitmap = new Bitmap(Controller.View_Width, Controller.View_Height);
                Graphics g = Graphics.FromImage(newBitmap);
                g.Clear(Color.White);
                for (int i = Controller.MyMap.Layer_Order.Count() - 1; i >=0; i--)
                {
                    if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i]-1].Layer_Type == CLayer.LayerType.CPointLayer)
                    {
                        g.DrawImage(((CPointLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpoint(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CLineLayer)
                    {
                        g.DrawImage(((CLineLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawlinelayer(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CPolygonLayer)
                    {
                        g.DrawImage(((CPolygonLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpolygon(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CTextLayer)
                    {
                        g.DrawImage(((CTextLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawtext(), 0, 0);                      
                    }
                }
                if (savebmp.FilterIndex == 0)
                {
                    newBitmap.Save(savebmp.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                else
                {
                    newBitmap.Save(savebmp.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            Transformer.Displayscale = displayscle;
            Transformer.OffsetX = offx;
            Transformer.OffsetY = offy;
            return true; 
        }

        static public bool SaveMap()
        {
            //保存地图文件,注意文件名
            FileStream WriteFile = new FileStream(Controller.MapPath+"\\"+Controller.mapname+".gzx", FileMode.Create);
            StreamWriter wFile = new StreamWriter(WriteFile);
            wFile.WriteLine(Controller.mapname+"  Grizzy-X Beta Version");
            wFile.WriteLine("比例尺 ");
            wFile.WriteLine("地图长 "+Controller.MyMap.Width+" 地图宽 "+Controller.MyMap.Height);
            wFile.WriteLine("单位 "+Controller.MyMap.Unit);
            wFile.WriteLine(Controller.MyMap.MapDes);
            wFile.WriteLine(Controller.MyMap.MapPath);
           
            //写入图例
            wFile.WriteLine(Controller.MapLegends.Count.ToString());
            for (int i = 0; i < Controller.MapLegends.Count; i++)
            {
                if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CPointLegend)
                {
                    CPointLegend temp = (CPointLegend)Controller.MapLegends[i];
                    wFile.WriteLine(temp.Legend_Type.ToString()+" "+temp.GetID().ToString()+" "+temp.Title+" "+temp.Subtitle
                    +" "+temp.Visible.ToString()+" "+temp.ByScale.ToString()+" "+temp.Point_Style.ToString()+" "+temp.color.ToArgb().ToString()+" "
                    +temp.size.ToString());
                }
                else if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CLineLegend)
                {
                    CLineLegend temp = (CLineLegend)Controller.MapLegends[i];
                    wFile.WriteLine(temp.Legend_Type.ToString() + " " + temp.GetID().ToString() + " " + temp.Title + " " + temp.Subtitle
                    + " " + temp.Visible.ToString() + " " + temp.ByScale.ToString() + " " + temp.Line_Style.ToString() + " " + temp.color.ToArgb().ToString() + " "
                    + temp.line_weight.ToString());
                }
                else if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CPolygonLegend)
                {
                    CPolygonLegend temp = (CPolygonLegend)Controller.MapLegends[i];
                    wFile.WriteLine(temp.Legend_Type.ToString() + " " + temp.GetID().ToString() + " " + temp.Title + " " + temp.Subtitle
                    + " " + temp.Visible.ToString() + " " + temp.ByScale.ToString() + " " + temp.Border_Style.ToString() + " " + temp.Fill_Style.ToString() + " "
                    + temp.border_weight.ToString()+" "+temp.border_color.ToArgb().ToString()+" "+temp.fill_color.ToArgb().ToString());
                }
                else if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CTextLegend)
                {
                    CTextLegend temp = (CTextLegend)Controller.MapLegends[i];
                    wFile.WriteLine(temp.Legend_Type.ToString() + " " + temp.GetID().ToString() + " " + temp.Title + " " + temp.Subtitle
                    + " " + temp.Visible.ToString() + " " + temp.ByScale.ToString() + " " + temp.string_Color.ToArgb().ToString()+" "+temp.font.Name.ToString()
                    + " " + temp.font.Size.ToString());
                }
            }

            wFile.WriteLine("图层总数 " + Controller.MyMap.Layer_List.Count.ToString());
            for (int i = 0; i < Controller.MyMap.Layer_List.Count; i++)
            {
                wFile.WriteLine(Convert.ToString(Controller.MyMap.Layer_List[i].GetID()) + " " + Controller.MyMap.Layer_List[i].GetName);
            }
            wFile.Close();
            WriteFile.Close();
            wFile.Dispose();
            WriteFile.Dispose();
            //图层显示顺序
            for (int i = 0; i < Controller.MyMap.Layer_List.Count; i++)
            {
                FileStream WhiteLayer = new FileStream(Controller.MapPath + "\\" + Controller.MyMap.Layer_List[i].GetName + ".lyr", FileMode.Create);
                StreamWriter wLayer = new StreamWriter(WhiteLayer);
                wLayer.WriteLine(Controller.MyMap.Layer_List[i].GetName + "  Grizzy-X Beta Version");
                wLayer.WriteLine(Controller.MyMap.Layer_List[i].Description);
                if(Controller.MyMap.Layer_List[i].Layer_Type==CLayer.LayerType.CPointLayer)
                {
                    wLayer.WriteLine("点图层");
                    List<CPoint> TempPointList=((CPointLayer)Controller.MyMap.Layer_List[i]).Point_Set;
                    wLayer.WriteLine(TempPointList.Count.ToString());
                    for (int j = 0; j < TempPointList.Count; j++)
                    {
                        //格式 ID 数目 图例ID
                        wLayer.WriteLine(TempPointList[j].GetID().ToString()+" "+TempPointList[j].points.Count.ToString()+" "+TempPointList[j].LegendID.ToString());
                        wLayer.WriteLine(TempPointList[j].MinBR.Lx.ToString() + " " + TempPointList[j].MinBR.Ly.ToString() + " "+TempPointList[j].MinBR.Rx.ToString()+" "+TempPointList[j].MinBR.Ry.ToString());
                        for (int k = 0; k < TempPointList[j].points.Count; k++)
                        {
                            wLayer.WriteLine(TempPointList[j].points[k].X.ToString()+" "+TempPointList[j].points[k].Y.ToString());                  
                        }
                    }
                }
                else if (Controller.MyMap.Layer_List[i].Layer_Type == CLayer.LayerType.CLineLayer)
                {
                    wLayer.WriteLine("线图层");
                    List<CLine> TempLineList = ((CLineLayer)Controller.MyMap.Layer_List[i]).Line_Set;
                    wLayer.WriteLine(TempLineList.Count.ToString());
                    for (int j = 0; j < TempLineList.Count; j++)
                    {
                        wLayer.WriteLine(TempLineList[j].GetID().ToString() + " " + TempLineList[j].Lines.Count.ToString()+" "+TempLineList[j].LegendID.ToString());
                        wLayer.WriteLine(TempLineList[j].MinBR.Lx.ToString() + " " + TempLineList[j].MinBR.Ly.ToString() + " " + TempLineList[j].MinBR.Rx.ToString() + " " + TempLineList[j].MinBR.Ry.ToString());
                        for (int k = 0; k < TempLineList[j].Lines.Count; k++)
                        {
                            for (int m = 0; m < TempLineList[j].Lines[k].Count; m++)
                            {
                                wLayer.WriteLine(TempLineList[j].Lines[k][m].X.ToString()+" "+TempLineList[j].Lines[k][m].Y.ToString());
                            }
                            wLayer.WriteLine("0");   //简单线之间空一行
                        }
                    }
                }
                else if (Controller.MyMap.Layer_List[i].Layer_Type == CLayer.LayerType.CPolygonLayer)
                {
                    wLayer.WriteLine("多边形图层");
                    List<CPolygon> TempPolygonList = ((CPolygonLayer)Controller.MyMap.Layer_List[i]).Polygon_Set;
                    wLayer.WriteLine(TempPolygonList.Count.ToString());
                    for (int j = 0; j < TempPolygonList.Count; j++)
                    {
                        wLayer.WriteLine(TempPolygonList[j].GetID().ToString() + " " + TempPolygonList[j].Polygons.Count.ToString() + " " + TempPolygonList[j].LegendID.ToString());
                        wLayer.WriteLine(TempPolygonList[j].MinBR.Lx.ToString() + " " + TempPolygonList[j].MinBR.Ly.ToString() + " " + TempPolygonList[j].MinBR.Rx.ToString() + " " + TempPolygonList[j].MinBR.Ry.ToString());
                        for (int k = 0; k < TempPolygonList[j].Polygons.Count; k++)
                        {
                            for (int m = 0; m < TempPolygonList[j].Polygons[k].Count; m++)
                            {
                                wLayer.WriteLine(TempPolygonList[j].Polygons[k][m].X.ToString() + " " + TempPolygonList[j].Polygons[k][m].Y.ToString());
                            }
                            wLayer.WriteLine("0");   //简单线之间空一行
                        }
                    }
                }
                else if (Controller.MyMap.Layer_List[i].Layer_Type == CLayer.LayerType.CTextLayer)
                {
                    wLayer.WriteLine("文本图层");
                    List<CText> TempTextList = ((CTextLayer)Controller.MyMap.Layer_List[i]).Text_Set;
                    wLayer.WriteLine(TempTextList.Count.ToString());
                    for (int j = 0; j < TempTextList.Count; j++)
                    {
                        wLayer.WriteLine(TempTextList[j].GetID().ToString() + " " + TempTextList[j].points.Count.ToString()+" "+TempTextList[j].LegendID.ToString());
                        wLayer.WriteLine(TempTextList[j].MinBR.Lx.ToString() + " " + TempTextList[j].MinBR.Ly.ToString() + " " + TempTextList[j].MinBR.Rx.ToString() + " " + TempTextList[j].MinBR.Ry.ToString());
                        
                        for (int k = 0; k < TempTextList[j].points.Count; k++)
                        {
                            wLayer.WriteLine(TempTextList[j].texts[k]+" "+TempTextList[j].points[k].X.ToString() + " " + TempTextList[j].points[k].Y.ToString());
                        }
                    }
                }
                wLayer.Close();
                WhiteLayer.Close();
                wLayer.Dispose();
                WhiteLayer.Dispose();
            }
            MessageBox.Show("保存完成！");
            return true;
        }

        //新建数据库,默认为与地图文件创建在同一文件夹下,并连接数据库
        static public bool CreateDatabase()
        {
           ADOX.Catalog catalog = new Catalog();
            catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Controller.MapPath + "\\test.mdb;Jet OLEDB:Engine Type=5");
            dbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Controller.MapPath +  "\\test.mdb";
            myConnection = new OleDbConnection(dbConnectionString);
            cmd = myConnection.CreateCommand();

            int result = InitializeMapTable();
            result = InitializeLayersTable(result); 
            return true;
        }


        //初始化一张存放所有图层属性数据的表
        private static int InitializeLayersTable(int result)
        {
            cmd.CommandText = "CREATE TABLE 已有图层属性数据" +
                " (" +
                " 图层ID int PRIMARY KEY," +
                " 图层名称 string(20) NOT NULL," +
                " 图层类型 string(15) NOT NULL," +
                " 描述 string(150)" +
                " );";
            myConnection.Open();
            result = cmd.ExecuteNonQuery();
            myConnection.Close();
            return result;
        }

        
        //初始化地图表
        private static int InitializeMapTable()
        {

            cmd.CommandText = "CREATE TABLE " + Controller.mapname +
                " (" +
                "MapID int PRIMARY KEY," +
                "地图名 string(20) NOT NULL," +
                "单位 string(10)," +
                "地图路径 string(50) NOT NULL," +
                "地图描述 string(150)" +
                ");";
            myConnection.Open();
            int result = cmd.ExecuteNonQuery();
            myConnection.Close();

            cmd.CommandText = "INSERT INTO " + Controller.mapname +
               " ( MapID, 地图名, 单位, 地图路径, 地图描述 )" +
               " VALUES (1, '" + Controller.mapname + "', '厘米', '" + Controller.MapPath + "', null);";
            //"VALUES(1 '地图' '厘米' 'c:\\' null);";
            myConnection.Open();
            result = cmd.ExecuteNonQuery();
            myConnection.Close();
            return result;

        }

        static public bool CreateTable()
        {
            //String commandText;
            return true;
        }

        static public bool AddField()
        {
            return true; 
        }

        static public bool DeleteField()
        {
            return true; 
        }

        static public bool GetAttribute()
        {
            return true;
        }

        static public bool AddGeoObject()
        {
            return true;
        }

        static public bool DeleteGeoObject()
        {
            return true;
        }

        static public bool ModifyAttribute()
        {
            return true;
        }

        
    }
}
